'
' Copyright (c) 2004
' by Peter Donker ( info@bring2mind.net )
'
' Permission is hereby granted to use and/or modify this software for a single server. It is 
' NOT permitted to publish, distribute, sublicense, and/or sell copies of this software in its
' original or modified form. You are not allowed to remove this copyright notice. Please read the
' enclosed EULA for further details.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
' BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
' DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THE SOFTWARE.
'
Imports DotNetNuke
Imports System.Drawing.Imaging

Namespace Bring2mind.DNN.Modules.UserFiles

 Friend Module Globals

  Public Sub ResizeImage(ByVal sFilename As String, ByVal iSize As Integer)
   Dim thisImage As Bitmap
   Dim imgFormat As ImageFormat
   Dim sFile$, sExt$, sDir$
   Dim imgHeight As Integer, imgWidth As Integer, imgRatio As Single

   Dim fi As System.IO.FileInfo
   Dim xProp As Integer

   fi = New System.IO.FileInfo(sFilename)
   sDir$ = fi.DirectoryName
   sExt$ = fi.Extension
   sFile$ = Left$(fi.Name, Len(fi.Name) - Len(sExt$))

   thisImage = New Bitmap(sFilename)
   imgFormat = thisImage.RawFormat
   imgWidth = thisImage.Width
   imgHeight = thisImage.Height
   imgRatio = imgHeight / imgWidth

   Dim backBuffer As Bitmap
   Dim backBufferGraphics As Graphics
   Dim xNewHeight As Integer, xNewWidth As Integer
   Dim xScale As Single

   If imgRatio > 1 Then
    xScale = iSize / imgHeight
   Else
    xScale = iSize / imgWidth
   End If
   If xScale > 1 Then
    fi = Nothing
    thisImage.Dispose()
    Exit Sub
   End If
   xNewHeight = Int(imgHeight * xScale)
   xNewWidth = Int(imgWidth * xScale)

   backBuffer = New Bitmap(xNewWidth, xNewHeight, Drawing.Imaging.PixelFormat.Format32bppRgb)
   backBufferGraphics = Graphics.FromImage(backBuffer)
   backBufferGraphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
   backBufferGraphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
   backBufferGraphics.DrawImage(thisImage, 0, 0, xNewWidth, xNewHeight)

   thisImage.Dispose()
   fi = Nothing
   backBuffer.Save(sFilename, imgFormat)
  End Sub
 End Module

End Namespace